package com.jz.pay.study;

import com.jz.pay.bo.PaymentBO;
import com.jz.pay.controller.PaymentController;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;

/**
 * this.registerBeanPostProcessors(beanFactory)
 */
@Component
public class BeanPostProcessImpl implements BeanPostProcessor {
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
//        if(beanName.contains("com.jz.pay")){
//            System.out.println("执行 BeanPostProcessor.postProcessBeforeInitialization....");
//        }
        if (bean instanceof PaymentController){
            System.out.println("执行 BeanPostProcessor.postProcessBeforeInitialization....初始化before");
//            System.out.println("PaymentBO Before");
//            ((PaymentBO) bean).test();
        }

        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
//        if(beanName.contains("com.jz.pay")){
//            System.out.println("执行 BeanPostProcessor.postProcessAfterInitialization....");
//        }

        if (bean instanceof PaymentController){
            ((PaymentController) bean).setPaymentService2(((PaymentController) bean).getPaymentService());
            System.out.println("执行 BeanPostProcessor.postProcessAfterInitialization....初始化after");
//            System.out.println("PaymentBO After");
//            ((PaymentBO) bean).test();
//            System.out.println("执行 beanName...."+beanName);

        }
        if(bean instanceof MessageListenerConcurrently){
            System.out.println("MessageListenerConcurrently: "+beanName);
        }
        return bean;
    }
}
